package pl.org.lutra.planner.gui.table;

import java.util.ArrayList;
import java.util.List;

import javax.swing.table.AbstractTableModel;

import pl.org.lutra.planner.model.data.Segment;

public abstract class PlannerTableModel<T extends Segment> extends AbstractTableModel {
    private static final long serialVersionUID = 5346539997423412990L;
    
    protected PlannerColumn<T>[] columns;
    protected List<T> segments = new ArrayList<T>();
    
    protected PlannerTableModel() {
        columns = createColumns();
    }
    
    public void setSegments(List<T> segments) {
        this.segments = segments;
    }
    
    protected abstract PlannerColumn<T>[] createColumns();
    
    @Override
    public int getRowCount() {
        if (segments != null) {
            return segments.size();
        } else {
            return 0;
        }
    }
    
    public PlannerColumn<T>[] getColumns() {
        return columns;
    }
    
    @Override
    public int getColumnCount() {
        return columns.length;
    }
    
    @Override
    public String getColumnName(int column) {
        return columns[column].getLabel();
    }
    
    @Override
    public Object getValueAt(int row, int column) {
        T segment = segments.get(row);
        return columns[column].getValue(segment);
    }
}
